package com.ruoyi.counter.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.counter.domain.Workordersub;
import com.ruoyi.counter.mapper.WorkorderMapper;
import com.ruoyi.counter.domain.Workorder;
import com.ruoyi.counter.service.IWorkorderService;

/**
 * 工单信息Service业务层处理
 * 
 * @author xll
 * @date 2021-08-02
 */
@Service
public class WorkorderServiceImpl implements IWorkorderService 
{
    @Autowired
    private WorkorderMapper workorderMapper;

    /**
     * 查询工单信息
     * 
     * @param id 工单信息ID
     * @return 工单信息
     */
    @Override
    public Workorder selectWorkorderById(Long id)
    {
        return workorderMapper.selectWorkorderById(id);
    }

    /**
     * 查询工单信息列表
     * 
     * @param workorder 工单信息
     * @return 工单信息
     */
    @Override
    public List<Workorder> selectWorkorderList(Workorder workorder)
    {
        return workorderMapper.selectWorkorderList(workorder);
    }

    /**
     * 新增工单信息
     * 
     * @param workorder 工单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkorder(Workorder workorder)
    {
        int rows = workorderMapper.insertWorkorder(workorder);
        insertWorkordersub(workorder);
        return rows;
    }

    /**
     * 修改工单信息
     * 
     * @param workorder 工单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkorder(Workorder workorder)
    {
        workorderMapper.deleteWorkordersubByWorkid(workorder.getId());
        insertWorkordersub(workorder);
        return workorderMapper.updateWorkorder(workorder);
    }

    /**
     * 批量删除工单信息
     * 
     * @param ids 需要删除的工单信息ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkorderByIds(Long[] ids)
    {
        workorderMapper.deleteWorkordersubByWorkids(ids);
        return workorderMapper.deleteWorkorderByIds(ids);
    }

    /**
     * 删除工单信息信息
     * 
     * @param id 工单信息ID
     * @return 结果
     */
    @Override
    public int deleteWorkorderById(Long id)
    {
        workorderMapper.deleteWorkordersubByWorkid(id);
        return workorderMapper.deleteWorkorderById(id);
    }

    /**
     * 新增工单明细信息
     * 
     * @param workorder 工单信息对象
     */
    public void insertWorkordersub(Workorder workorder)
    {
        List<Workordersub> workordersubList = workorder.getWorkordersubList();
        Long id = workorder.getId();
        if (StringUtils.isNotNull(workordersubList))
        {
            List<Workordersub> list = new ArrayList<Workordersub>();
            for (Workordersub workordersub : workordersubList)
            {
                workordersub.setWorkid(id);
                list.add(workordersub);
            }
            if (list.size() > 0)
            {
                workorderMapper.batchWorkordersub(list);
            }
        }
    }
}
